using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

namespace Adventure
{
    public class UIButton : Button
    {
        private Vector3 _presetScale = -Vector3.one;
        private Vector3 _downScale = Vector3.one;
        public bool ignoreTouchedScale { get; set; }  // 是否禁用点击缩放设置
        private float _touchScale;
        public float TouchedScale
        {
            get { return _touchScale; }
            set { _touchScale = value; }
        }  
       
        protected override void Awake()
        {
            base.Awake();
            //this.transition = Transition.None;
            this.TouchedScale = 1.1f;  // 默认点击缩放值
        }

        public override void OnPointerDown(PointerEventData eventData)
        {
            base.OnPointerDown(eventData);
            if (!ignoreTouchedScale && this.interactable)
            {
                if (_presetScale == -Vector3.one)
                {
                    _presetScale = this.gameObject.transform.localScale;
                    _downScale = _presetScale * TouchedScale;
                }
                this.gameObject.transform.localScale = _downScale;
            }
        }

        public override void OnPointerUp(PointerEventData eventData)
        {
            base.OnPointerUp(eventData);
            if (!ignoreTouchedScale && this.interactable)
            {
                // 如果在 PointerDown 和 PointerUp 之间修改过 localScale 的值，就不再还原设置。
                if (this.gameObject.transform.localScale == _downScale)
                {
                    this.gameObject.transform.localScale = _presetScale;
                }
            }
        }       
    }
}